home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / 3DDEMO.ZIP / 3D / INCLUDE / MESH.HPP < prev    next >
C/C++ Source or Header  |  1996-07-23  |  7KB  |  203 lines

  1. #ifndef __MESH__
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. #include "defines.hpp"
  9. #include "polygfx.hpp"
  10.  
  11. // Copyright (c) 1996 by Kerrigan Burgess, all rights reserved.
  12.  
  13. extern int   HALF_SCREEN_WIDTH;
  14. extern int   HALF_SCREEN_HEIGHT;
  15. extern float HALF_SCREEN_WIDTH_VD;
  16. extern float HALF_SCREEN_HEIGHT_VD;
  17. extern int   YON_Z;
  18. extern int   HITHER_Z;
  19.  
  20. extern void Error(char *fmt, ...);
  21.  
  22. const int CENTER    = 0;             // just some define statements.
  23. const int VERTICES  = 1;
  24.  
  25. typedef float MATRIX[4][4];
  26.  
  27. struct tempstruct {     // used for calculating avgnormals for polygons.
  28.   int p0,p1,p2;         // and other stuff!
  29. };
  30.  
  31. typedef struct _3dpointyp {
  32.     float x,y,z;
  33.     float morphx,morphy,morphz;
  34.     float morphdx,morphdy,morphdz;      // used for morphing.
  35.     int u,v;
  36.     int Intensity;       // for Gouraud(Phong) Shading.
  37.     int valid;         // if this point is valid. (used for Morphing.)
  38. } POINT3D;
  39.  
  40. class POLYGONCLASS {
  41.  
  42.  friend class FILESYSTEMCLASS;
  43.     
  44.  public:
  45.  
  46.     #define MAX_VERTEX 3
  47.  
  48.     POINT3D   *Vertex[MAX_VERTEX];    // holds the vertices of object, pointer to actual camera coords in main object.
  49.     POINT3D   *AvgNormal[MAX_VERTEX]; // holds pointer to avgnormal point in camera coords.
  50.     POINT3D    Normal;                // holds normal for this frame. (used for shadow mapping.)
  51.     POINT3D    shadow[MAX_VERTEX];    // holds info for shadow.
  52.     
  53.     float      Normalength;           // length of the normal.
  54.     float      avgz;                  // holds precomputed avg z for depth sort.
  55.     int        u0,v0,u1,v1,u2,v2;     // holds the uv coords for polygon.
  56.     int        color;                 // holds the color of the polygon.
  57.     int        visible;               // is polygon visible?
  58.     int        shadowcolor;           // holds the shadow color.
  59.     int        shadowvisible;         // is shadow visible?
  60.     unsigned char *texture;           // pointer to texture for this polygon.
  61.  
  62.     POLYGONCLASS(void);
  63.     ~POLYGONCLASS(void);
  64. };
  65.  
  66. int PolyCompare(const void **arg1, const void **arg2);  // compare function for qsort.
  67. void ComputeAvgZ(void);
  68.  
  69. class LIGHTCLASS {
  70.     private:
  71.       POINT3D lightsource;           // light vector.
  72.       POINT3D infinitelightsource;
  73.  
  74.     public:
  75.       LIGHTCLASS(void);
  76.       ~LIGHTCLASS(void);
  77.  
  78.       SetLight(float x, float y, float z);
  79.       POINT3D *GetLight(void);      
  80.       POINT3D *GetInfiniteLight(void);
  81. };
  82.  
  83. class CAMERACLASS {
  84.     private:
  85.       POINT3D viewpoint;
  86.       int angx,angy,angz;
  87.  
  88.     public:
  89.       CAMERACLASS(void);
  90.       ~CAMERACLASS(void);
  91.  
  92.       void SetViewPoint(float viewx,float viewy,float viewz);
  93.       void MoveViewPoint(float viewx,float viewy,float viewz);
  94.       void SetViewAngle(int angle_x,int angle_y,int angle_z);
  95.       void MoveViewAngle(int angle_x,int angle_y,int angle_z);
  96.       POINT3D *GetViewPoint(void);
  97.       void CreateCameraMatrix(MATRIX CameraMatrix);
  98. };
  99.  
  100. class OBJECTCLASS {
  101.  
  102.  friend class FILESYSTEMCLASS;
  103.  
  104.  public:
  105.     char  name[20];                // name of object.
  106.     int   ObjectID;                // id of object.
  107.     int   numpoly;                 // how many polys make up object.
  108.     int   numvertices;             // how many vertices make up object.
  109.    
  110.     float worldx,worldy,worldz;    // position of object in world.
  111.     float maxz;
  112.     float radius;                  // radius of object.
  113.     float xsphere,ysphere,zsphere; // center coords, in camera space.
  114.     float morphsteps;              // num of steps to morph this object in.
  115.  
  116.     POINT3D      *LocalCoord;
  117.     POINT3D      *CameraCoord;
  118.  
  119.     POLYGONCLASS *Polygon;
  120.     OBJECTCLASS  *NextObject;
  121.     OBJECTCLASS  *MorphSource;         // source object to start next morphing.
  122.     OBJECTCLASS  *NextMorphObject;     // circular list.    
  123.     OBJECTCLASS(void);
  124.     ~OBJECTCLASS(void);
  125.  
  126.     void PolyCull(int Mode);
  127.     void TransformObject(MATRIX TSR_Matrix);
  128.     void Local2Camera(MATRIX CameraMatrix, int Mode);
  129.     OBJECTCLASS *CreateMorphData(void);
  130.     OBJECTCLASS *GetMorphObject(void);
  131.     void HSR_Shade(POINT3D *viewpoint, POINT3D *lightsource);
  132.     void DoShadows(POINT3D *lightsource);
  133.     void GetCenterStats(float *radius, float *xsphere, float *ysphere, float *zsphere);
  134.     void ComputeNormalength(int vertex0, int vertex1, int vertex2,POLYGONCLASS *ThisPolygon);
  135.     void PreComputeNormal(int vertex0, int vertex1, int vertex2,int index);
  136.     void PreComputeAvgNormal(struct tempstruct *polystats);
  137.     void ComputeRadius(void);
  138.     void FindCenter(void);
  139. };
  140.  
  141. class MESHCLASS {
  142.  
  143.  friend class FILESYSTEMCLASS;
  144.  
  145.     public:
  146.  
  147.       int MAX_LIGHTS;
  148.       int MAX_CAMERAS;
  149.       int velangx,velangy,velangz;       // the velocity of rotation angles.
  150.       int angx,angy,angz;                // angles to rotate object.
  151.       int tx,ty,tz;                      // translation vectors.
  152.       int translevel;                    // level of transparency.
  153.       int totalfaces;                    // used to allocate MeshList.
  154.  
  155.       int   _CURRENTCAMERA_;             // default camera.
  156.       
  157.       MATRIX CameraMatrix;
  158.       MATRIX TSR_Matrix;
  159.  
  160.       OBJECTCLASS *ObjectHead;
  161.       LIGHTCLASS  *Light;
  162.       CAMERACLASS *Camera;
  163.  
  164.       MESHCLASS(void);
  165.      ~MESHCLASS(void);
  166.  
  167.       int MaxTransLevel;                 // how many transparency levels.
  168.  
  169.       void  CreateMeshList(void);
  170.       void  ResetOrigin(void);
  171.       void  SetTransparencyLevel(int delta);
  172.       void  SetMaxTransparency(int Levels);
  173.       void  SetShading(int Shade);
  174.       void  SetTextureMapping(int mode);
  175.       void  SetMorph(int mode);
  176.       void  SetShadow(int mode);
  177.       void  SetHazing(int mode);
  178.       void  SetTransparency(int mode);
  179.       void  SetCurrentCamera(int camera);
  180.       void  MoveCamera(int angle_x,int angle_y,int angle_z,float viewx,float viewy,float viewz);
  181.       void  SetTSRVectors(int angle_x,int angle_y,int angle_z,int transx,int transy,int transz);
  182.       void  ResetTSRVectors(void);
  183.       void  CreateTSR_Matrix(void);
  184.       int   SetWorldXYZ(int ObjectID,float wx,float wy,float wz);
  185.       void  ObjectCull(int Mode);
  186.       void  Push(OBJECTCLASS *ThisObject, int Status);
  187.       void  _3DPipeLine(void);
  188.       void  DepthSort(void);
  189.       void  Render(void);
  190. };
  191.  
  192.   void  MakeVector(POINT3D *init, POINT3D *term, POINT3D *vector);
  193.   void  CrossProduct(POINT3D *u, POINT3D *v, POINT3D *normal);
  194.   float Magnitude(POINT3D *vector);
  195.   void  Normalize(POINT3D *vector);
  196.   float DotProduct(POINT3D *u, POINT3D *v);
  197.   void  CopyPoint3D(POINT3D *source, POINT3D *dest);
  198.   void  BenchMark(int type);
  199.  
  200. #define __MESH__
  201. #endif
  202.  
  203.